// https://segmentfault.com/a/1190000018429380

import { DB_NAME, DB_SCHEMA, DB_VERSION } from './config'
import { IDB } from '../db/index'
import { DBServiceLog } from './log'
import { DBServiceMsg1 } from './msg1'

// const LOG_STORE_NAME = 'log'

export class DBController {
  private idb: IDB
  log: DBServiceLog
  msg1: DBServiceMsg1

  constructor() {
    this.idb = new IDB(DB_NAME, DB_VERSION, DB_SCHEMA)
    this.log = new DBServiceLog(this.idb)
    this.msg1 = new DBServiceMsg1(this.idb)
  }

  async open() {
    await this.idb.open()
    // 每次打开就清理三日前的数据
    // const iStore = this.idb.getStore(LOG_STORE_NAME)
    // if (!iStore) throw new Error('DB not ready')
    // try {
    //   await iStore.bulkDelete({
    //     keyName: 'time',
    //     lower: 0,
    //     upper: Date.now() - 3 * 24 * 60 * 60 * 1000
    //     // upper: Date.now() - 10 * 1000
    //   })
    // } catch (_err) {
    //   // 忽略删除失败的错误
    // }
  }
}
